package com.zzuli.practice.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzuli.practice.entity.Repairman;
import com.zzuli.practice.entity.User;
import com.zzuli.practice.mapper.WorkerMapper;
import com.zzuli.practice.service.IWorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WorkerService implements IWorkerService {

    @Autowired
    private WorkerMapper workerMapper;

    public IPage<Repairman> pageWorkList(Integer pageIndex){
        if (pageIndex == null || pageIndex < 1){
            pageIndex = 1;
        }
        // 首先需要定义一个拦截器才能使用
        Page<Repairman> page = new Page<>(pageIndex, 5);

        IPage<Repairman> iPage = workerMapper.selectPage(page, null);
        // 判断当前页是否大于总页数
        if (iPage.getCurrent() > iPage.getPages()){
            page.setCurrent(iPage.getPages());
        }
        return workerMapper.selectPage(page, null);
    }

    public Repairman selectByName(String name){
        return workerMapper.selectByName(name);
    }

    public int insertWorker(Repairman repairman){
        return workerMapper.insert(repairman);
    }

    public int updateForStatusById(Integer id, Integer status){
        UpdateWrapper<Repairman> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", status);
        return workerMapper.update(null, updateWrapper);
    }

    public Repairman selectById(Integer id){
        return workerMapper.selectById(id);
    }

    // 删除维修员
    public int deleteById(Integer id){
        return workerMapper.deleteById(id);
    }

    public int updateById(Integer id, Repairman repairman){
        UpdateWrapper<Repairman> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        return workerMapper.update(repairman, wrapper);
    }

}
